package me.hl.math;

import org.junit.Test;

import java.math.BigDecimal;

/**
 * Created by Planck on 2016/2/5.
 */
public class MathDemo {

    @Test
    public void bigDecimalDemo() {
        BigDecimal a = new BigDecimal("1.22");
        System.out.println("construct with a String value: " + a);
        BigDecimal b = new BigDecimal("2.22");
        a = a.add(b); //这里是新建了一个对象。所有需要赋值
        System.out.println("a plus b is : " + a);
    }

    @Test
    public void commonDemo() {
        //向下取正
        System.out.println("Math.floor(10.5)=" + Math.floor(10.5));   //10
        System.out.println("Math.floor(-10.5)=" + Math.floor(-10.5));  //-11
        System.out.println("Math.floor(-0.0)=" + Math.floor(-0.0));   //-0.0

        //向上取正
        System.out.println("Math.ceil(-10.1)=" + Math.ceil(-10.1));   //-10.0
        System.out.println("Math.ceil(10.7)=" + Math.ceil(10.7));    //11.0
        System.out.println("Math.ceil(-0.0)=" + Math.ceil(-0.0));    //-0.0

        //取得一个大于或者等于0.0小于不等于1.0的随机数
        System.out.println("Math.random()=" + Math.random());

        //round 四舍五入，float时返回int值，double时返回long值
        System.out.println("Math.round(10.1)=" + Math.round(10.1));   //10
        System.out.println("Math.round(10.7)=" + Math.round(10.7));   //11
    }

}
